package com.dhecp.project.finance.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.core.utils.ReadExcel;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.finance.domain.AccountCountForCompany;
import com.dhecp.project.finance.domain.AccountDetailedForCompanyDJ;
import com.dhecp.project.finance.service.AccountCountForCompanyService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 公司实时资金 控制层
 * @author DH-Xxj
 * @version 2022年10月19日
 */
@Controller
@RequestMapping("/finance/accountcountforcompany")
public class AccountCountForCompanyController extends BaseModuleController{

    private String moduleTable = "VCW_ACCOUNTCOUNTFORCOMPANY";
    
    @Autowired
    private AccountCountForCompanyService businessService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    /**
     * 打开查询列表页
     * @param params
     * @return
     * @author DH-Xxj 
     */
	@RequiresPermissions("finance:accountcountforcompany:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new AccountCountForCompany()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("prefix","finance/finance"); //页面路径
        params.put("toolBarWidth", 240);
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("finance", "accountcountforcompany");
    	mv.addObject("type", "company");
		return mv;
    }


    /**
     * 打开制单页
     *
     * @param nid 必要参数：单据编码（新增时传默认值 0 ，修改、查看时传单据实际值）
     * @return
     * @author DH-Xxj
     */
    @GetMapping("/countForCompany")
    public ModelAndView bindShop(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        params.put("entity", new AccountDetailedForCompanyDJ()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        List<AccountDetailedForCompanyDJ> infoItem = new ArrayList<>();
        try {
            infoItem = businessService.findByDj(params.get("vGsbm").toString());

        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("countForCompany", infoItem);
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName("finance/accountcountforcompany/countForCompany");
        return mv;
    }


	/**
	 * 查询页 列表数据
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
    @RequiresPermissions("finance:accountcountforcompany:list")
	@GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
        List<AccountCountForCompany> list = new ArrayList<AccountCountForCompany>();
        try {
        	list = businessService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }

    /**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("finance:accountcountforcompany:export")
    @Log(title = "公司实时资金导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new AccountCountForCompany()); 
    	
        List<AccountCountForCompany> list = businessService.queryList(params);
        
    	params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<AccountCountForCompany> util = new ExportExcelUtils<AccountCountForCompany>(moduleTable,new AccountCountForCompany());
        return util.exportExcel(list, fieldList, "公司实时资金", "export");
    }

    /**
     * @Description: 修改公司负债额度
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-17 14:55
     */
    @RequiresPermissions("finance:accountcountforcompany:edit")
    @Log(title = "修改公司负债额度", businessType = BusinessType.UPDATE)
    @PostMapping("/updateType")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateType(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = businessService.updateType(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * @Description: 修改公司负债额度
     */
    @GetMapping("/updateTypeLocal")
    @ResponseBody
    public AjaxResult updateTypeLocal() {

    	AjaxResult ajaxResult = new AjaxResult();

        try {
	    	List<List<String>> rows = ReadExcel.getRowsByFile("d:\\Users\\Administrator\\Desktop\\导入ERP\\公司信息.xlsx", 3);
	        for (int i = 1; i < rows.size(); i++) {

	        	List<String> row = rows.get(i);
	        	Map<String, Object> params = new HashMap<String, Object>();
	            params.put("moduleTable", moduleTable);
	            
	            AccountCountForCompany accountCountForCompany = businessService.findByShortName(row.get(0));
	            if (accountCountForCompany == null) {
					continue;
				}
	        	
	        	JSONObject JO = new JSONObject();
	        	JO.put("guid", accountCountForCompany.getGuid());
	        	JO.put("fFzEd", row.get(3));
	        	params.put("infoItem", JO.toJSONString());
	            ajaxResult = businessService.updateType(params);
	        }
    	
        }catch (Exception e) {
        	e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}
